package xj.graph2d.command;

import java.util.Iterator;

import xj.graph2d.CompoundDrawShape;
import xj.graph2d.DrawShape;
import xj.graph2d.SimpleDrawDocument;
import xj.graph2d.SimpleDrawView;

public class RemoveShapesCommand extends AbstractCommand {

  public RemoveShapesCommand(SimpleDrawView view, SimpleDrawDocument doc,
      CompoundDrawShape shapes) {
    super(view, doc);
    this.shapes = shapes;
  }

  public void execute() {
    if (shapes != null) {
      Iterator iter = shapes.allShapes();
      while (iter.hasNext()) {
	DrawShape s = (DrawShape) iter.next();
	doc.remove(s);
      }
      shapes.calculateBounds();
      canvas.repaintRect(shapes.getBounds());
    }
  }

  public void undo() {
    if (shapes != null) {
      DrawShape selected = doc.getSelectedShape();
      doc.setSelectedShape(null);
      canvas.repaintShape(selected);
      Iterator iter = shapes.allShapes();
      while (iter.hasNext()) {
	DrawShape s = (DrawShape) iter.next();
	doc.add(s);
      }
      shapes.calculateBounds();
      canvas.repaintRect(shapes.getBounds());
    }
  }

  protected CompoundDrawShape shapes;

}
